package com.bosi.exmple.chat;

import com.bosi.exmple.chat.entity.WSRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedHashMap;
import java.util.function.Function;

/**
 * 消息服务允许注册消息处理器
 */
@Slf4j
public class MessageCenter extends LinkedHashMap<Object, Function<WSRequest,Object>> {

    Function<WSRequest, Object> defHandler = v -> null;

    @Override
    public Function<WSRequest, Object> put(Object key, Function<WSRequest, Object> value) {
        log.info("注册消息处理器：{} -> {}",key,value.getClass().getName());
        return super.put(key, value);
    }

    @Override
    public Function<WSRequest, Object> get(Object key) {
        Function<WSRequest, Object> handler = super.get(key);
        if(handler == null){
            log.warn("未找到消息处理器,key = {}",key);
            return defHandler;
        }else {
            return handler;
        }
    }
}
